package chixing.day20.work1;

import java.util.*;


public class MemberService {
    private static List<Member> members = new ArrayList<>();
    public void addMember(Member member) {
        members.add(member);
    }
    //根据入住记录为会员累计积分
    public void recordStay(OrderRecord record){
     Member member = getMemberById(record.getLiverid());
     int score = calculateScore(record.getRoomtype(),record.getDays());
     member.setScore(member.getScore()+score);
 }

    public Member getMemberById(int id) {
        for (Member member : members) {
            if (member.getId() == id) {
                return member;
            }
        }
        return null;
    }
    //获取积分最高的N位会员
    public List<Member> getTopNMembers(int n) {
        List<Member> sortedMembers = new ArrayList<>();
        Collections.sort(members, new Comparator<Member>() {
            @Override
            public int compare(Member m1, Member m2) {
                return m2.getScore() - m1.getScore();
            }
        });
        for (int i = 0; i < n && i < members.size(); i++) {
            sortedMembers.add(members.get(i));
        }
        return sortedMembers;

    }
    //按会员等级分组（青铜<2000，白银<5000，黄金≥5000）
    public Map<String, List<Member>> groupByLevel() {
        Map<String, List<Member>> groupedMembers = new HashMap<>();
        groupedMembers.put("青铜", new ArrayList<>());
        groupedMembers.put("白银", new ArrayList<>());
        groupedMembers.put("黄金", new ArrayList<>());

        for (Member member : members) {
            int score = member.getScore();
            if (score < 2000) {
                groupedMembers.get("青铜").add(member);
            } else if (score < 5000) {
                groupedMembers.get("白银").add(member);
            } else {
                groupedMembers.get("黄金").add(member);
            }
        }

        return groupedMembers;
    }
    public int calculateScore(String roomType,int days){
        switch (roomType){
            case "普通房":
                return days*10;
            case "高级房":
                return days*20;
            case "套房":
                return days*30;
            default:
                return 0;
        }
    }

}